package com.shenzhen.horselive.ui.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.flqy.baselibrary.utils.L;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;
import com.shenzhen.horselive.R;
import com.shenzhen.horselive.User;
import com.shenzhen.horselive.adapter.OpenHistoryAdapter;
import com.shenzhen.horselive.api.ApiFactory;
import com.shenzhen.horselive.base.AppBarActivity;
import com.shenzhen.horselive.beans.ApplyHistory;
import com.shenzhen.horselive.databinding.ActivityOpenCardHistoryBinding;
import com.shenzhen.horselive.network.SchedulersCompat;
import com.shenzhen.horselive.presenter.LoadingViewObserver;

import java.util.ArrayList;
import java.util.List;

public class OpenCardHistoryActivity extends AppBarActivity {

    ActivityOpenCardHistoryBinding  binding ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding =ActivityOpenCardHistoryBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        setStyleDark();
        setTitle("预约历史");
        initViews();
//        initData();
    }

    @Override
    protected void onResume() {
        super.onResume();
        initData();
    }

    OpenHistoryAdapter   historyAdapter;
    private void initViews() {
        binding.recyclerview.setLayoutManager(new LinearLayoutManager(this));
          historyAdapter= new OpenHistoryAdapter(R.layout.item_open_card_history);
        binding.recyclerview.setAdapter(historyAdapter);
        //空布局
        historyAdapter.setEmptyView(R.layout.no_data_layout);
        historyAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {

//                * 1=>’预约中’,2=>’已失效’,0=>’待提交’,3=>’待邮寄’,4=>’已邮寄’ ,5=>’’权限未开通‘,6=>’’已取消’
                 int  state = historyAdapter.getItem(position).getOrderStatus();
                 //别的状态不给看
                 switch (state)
                 {
                     case 0:
                     case 1:
                     case 3:
                     case 4:
                     case 5:
                     case 6://取消状态不给看
                         OpenCardDieatilsActivity.start(OpenCardHistoryActivity.this,historyAdapter.getItem(position).getOrderCode());
                         break;
                 }



            }
        });

        binding.smartRefresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                initData();
            }
        });

    }


    private void initData() {
        ApiFactory.getApiService().applyHistory(User.get().getMyInfo().getToken())
                .compose(SchedulersCompat.applyIoSchedulers())
                .safeSubscribe(new LoadingViewObserver<ApplyHistory>(this) {
                    @Override
                    protected void onNextInActive(ApplyHistory applyHistory) {
                        if (binding.smartRefresh.isRefreshing())
                        {
                            binding.smartRefresh.finishRefresh();
                        }
                        if (applyHistory.isSuccess())
                        {
                            historyAdapter.setNewInstance(applyHistory.getData());

                        }else
                        {
                            showToast(applyHistory.getErrMessage());
                        }
                    }
                });


    }
}